package com.yyfax.ph.personal.subaccount;

import android.text.TextUtils;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class SubAccountPresenter extends Presenter<SubAccountModel, SubAccountPresenter.View> {

	private static final String TAG = SubAccountPresenter.class.getSimpleName();

	private boolean mStatus = true;//状态类型：true.下拉刷新  false.上拉加载

	public static class Generator implements PresenterFactory<SubAccountPresenter> {
		@Override
		public SubAccountPresenter create() {
			return new SubAccountPresenter();
		}
	}

	private View mView;

	@Override
	public void attach(SubAccountPresenter.View view) {
		mView = view;
		mModel = new SubAccountModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public interface View extends IView {
		void refreshSuccess(int count, int applyCount, List<SubAccountModel.SubAccountBean> list);

		void loadmoreSuccess(List<SubAccountModel.SubAccountBean> list);

		void getSubAccountFailure(String tips);
	}

	public void getSubAccounts(int pageNo, int pageSize, boolean status) {
		mStatus = status;
		execute(new SubAccountModel.SubAccount(pageNo, pageSize), FUNCTIONAL_CODE, false);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void subAccountResult(SubAccountModel.SubAccountResult result) {
		CLog.i(TAG, "result = " + result.toString());
		if (isResponseSuccess(result)) {
			if (mStatus)
				mView.refreshSuccess(result.getContent().subCount, result.getContent().subApplyCount, result.getContent().subList);
			else
				mView.loadmoreSuccess(result.getContent().subList);

		} else {
			mView.getSubAccountFailure(TextUtils.isEmpty(result.getMsg()) ? "获取子账户数据失败!" : result.getMsg());
		}
	}
}
